# file: multicore_test10.py
from machine import SPI,Pin
from ssd1306 import SSD1306_SPI
import framebuf
import _thread
import utime

spi = SPI(1, 100000, mosi=Pin(11), sck=Pin(10))
display = SSD1306_SPI(128, 64, spi, Pin(9),Pin(8), Pin(1))

def run_on_core1():
    global fbuf
    while True:
        if not fbuf == None:
            display.fill(0)

            lock.acquire()
            display.blit(fbuf,19,0)
            fbuf = None
            lock.release()

            display.show()

        utime.sleep_ms(100)

def run_on_core0():
    global fbuf
    while True:
        for i in range(1,356):
            dirt = 'BAD_APPLE/' + str(i) + '.pbm'
            print(i, dirt)
            with open(dirt,'rb') as f :
                f.readline()
                f.readline()
                data = bytearray(f.read())

                lock.acquire()
                fbuf = framebuf.FrameBuffer(data,88,64,framebuf.MONO_HLSB)
                lock.release()

                utime.sleep_ms(100)

fbuf = None
lock = _thread.allocate_lock()
_thread.start_new_thread(run_on_core1, ( ))
run_on_core0()